package com.codedancer.campus.common.dataresource;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.codedancer.campus.common.model.tx.HeadlineNewsModel;
import com.codedancer.campus.common.model.tx.MobileLocalModel;
import java.util.Arrays;
import java.util.List;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

/**
 * 天行数据
 */
@Component
@Slf4j
public class TianXinData {

    // 远程调用
    @Autowired
    private RestTemplate restTemplate;

    // 调用天行数据的接口key
    @Value("${start.campus.tianxin.appKey}")
    private String txAppKey;

    // 四个直辖市
    private static final String[] CROWN_CITY = {"北京", "上海", "天津", "重庆"};

    // 基础接口
    private final String BASE_URL = "https://apis.tianapi.com";

    // 获取手机号码归属地接口
    private final String MOBILE_LOCAL = BASE_URL + "/mobilelocal/index?";
    // 获取今日头条接口
    private final String TOP_NEWS = BASE_URL + "/topnews/index?";
    // ip地址查询
    private final String IP_QUERY = BASE_URL + "/ipquery/index?";

    /**
     * 获取号码归属地
     *
     * @return
     */
    public MobileLocalModel getMobileLocal(String phone) {
        String params = "key=" + txAppKey + "&phone=" + phone;
        log.info("请求地址：{}", MOBILE_LOCAL + params);
        ResponseEntity<String> entity = restTemplate.getForEntity(MOBILE_LOCAL + params,
            String.class);
        log.info("{}", entity.getBody());
        JSONObject jsonObject = JSONUtil.parseObj(entity.getBody());
        MobileLocalModel mobileLocalModel = BeanUtil.toBean(jsonObject.get("result"),
            MobileLocalModel.class);
        log.info("mobileLocal：{}", mobileLocalModel);
        return mobileLocalModel;
    }

    /**
     * 获取头条新闻
     *
     * @return
     */
    public List<HeadlineNewsModel> getHeadlineNews() {
        String params = "key=" + txAppKey + "&num=50";
        log.info("今日头条新闻请求地址：{}", TOP_NEWS + params);
        ResponseEntity<String> responseEntity = restTemplate.getForEntity(TOP_NEWS + params,
            String.class);
        JSONObject jsonObject = JSONUtil.parseObj(responseEntity.getBody());
        if (Integer.parseInt(jsonObject.get("code").toString()) == 200) {
            // 获取结果
            Object result = jsonObject.get("result");
            JSONObject resultObject = JSONUtil.parseObj(result);
            List<HeadlineNewsModel> headlineNewsModels = JSONUtil.toList(
                resultObject.get("list").toString(),
                HeadlineNewsModel.class);
            return headlineNewsModels;
        }
        return null;
    }

    /**
     * 根据ip获取到地址
     *
     * @param ip
     * @return
     */
    public String getLocationByIp(String ip) {
        String params = "key=" + txAppKey + "&ip=" + ip;
        log.info("ip地址查询请求地址：{}", IP_QUERY + params);
        ResponseEntity<String> responseEntity = restTemplate.getForEntity(IP_QUERY + params,
            String.class);
        JSONObject jsonObject = JSONUtil.parseObj(responseEntity.getBody());
        if (Integer.parseInt(jsonObject.get("code").toString()) == 200) {
            // 获取结果
            Object result = jsonObject.get("result");
            JSONObject resultObject = JSONUtil.parseObj(result);
            // 获取省级行政区
            String province = resultObject.get("province").toString();
            // 获取城市
            String city = resultObject.get("city").toString();
            if (Arrays.asList(CROWN_CITY).contains(province)) {
                // 属于直辖市
                return city;
            } else if (ObjectUtil.isNotEmpty(province) && ObjectUtil.isNotEmpty(city)) {
                return province + "省" + "_" + city + "市";
            } else {
                return null;
            }
        }
        return null;
    }
}
